package com.example.administrator.testproject.flagTextview;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

/**
 * Created by shengfh on 2018/1/16.
 */

public class LayoutCust extends LinearLayout {
    public LayoutCust(Context context) {
        super(context);
    }

    public LayoutCust(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public LayoutCust(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


//    @Override
//    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
////        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//        Log.d("Tag", "onMeasure");
//        // find the current child view
//        View view = getChildAt(0);
//        if (view != null) {
//
//            // measure the current child view with the specified measure spec
//            view.measure(widthMeasureSpec, heightMeasureSpec);
//        }
//
//        setMeasuredDimension(getMeasuredWidth(), measureHeight(heightMeasureSpec, view));
//    }

    /**
     * Determines the height of this view
     *
     * @param measureSpec A measureSpec packed into an int
     * @param view the base view with already measured height
     *
     * @return The height of the view, honoring constraints from measureSpec
     */
//    private int measureHeight(int measureSpec, View view) {
//
//        int result = 0;
//        int specMode = MeasureSpec.getMode(measureSpec);
//        int specSize = MeasureSpec.getSize(measureSpec);
//
//        if (specMode == MeasureSpec.EXACTLY) {
//            result = specSize;
//        } else {
//            // set the height from the base view if available
//            if (view != null && view instanceof FlagTextView) {
//                result = ((FlagTextView)view).getHHeight();
//                Log.e("Tag",result+"result");
//            }
////            if (specMode == MeasureSpec.AT_MOST) {
////                result = Math.min(result, specSize);
////            }
//        }
//        Log.e("Tag",result+"--result");
//        return result;
//    }
//    /*
//       *测量方法，测量父布局的宽度和高度
//       */
//    @Override
//    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        removeAllViews();
//        for (int i = 0; i < getChildCount(); i++) {
//            View child = getChildAt(i);
//            addView(child);
//        }
//        //重新设置宽高
//        this.setMeasuredDimension(measureWidth(widthMeasureSpec, heightMeasureSpec), measureHeight(widthMeasureSpec, heightMeasureSpec));
//    }

    /**
     * 测量宽度
     */
    private int measureWidth(int widthMeasureSpec, int heightMeasureSpec) {
        // 宽度
        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
        int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
        //父控件的宽（wrap_content）
        int width = 0;
        int childCount = getChildCount();

        //重新测量子view的宽度，以及最大高度
        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
            measureChild(child, widthMeasureSpec, heightMeasureSpec);
            // LayoutParams lp = child.getLayoutParams();
      /* MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
      int childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
      width += childWidth;*/
            width += child.getMeasuredWidth();
        }
        return modeWidth == MeasureSpec.EXACTLY ? sizeWidth : width;
    }


    /**
     * 测量高度
     */
    private int measureHeight(int widthMeasureSpec, int heightMeasureSpec) {
        //高度
        int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
        int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
        //父控件的高（wrap_content）
        int height = 0;
        int childCount = getChildCount();

        //重新测量子view的宽度，以及最大高度
        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
            measureChild(child, widthMeasureSpec, heightMeasureSpec);
      /* MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
      int childHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
      height += childHeight;*/
            height += child.getMeasuredHeight();
        }
        height = height / childCount;
        return modeHeight == MeasureSpec.EXACTLY ? sizeHeight : height;
    }

    /**
     * 给子布局设定位置
     */
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childLeft = 0;//子View左边的间距
        int childWidth;//子View的宽度
        int height = getHeight();//屏幕的宽度
        int childCount = getChildCount();//子View的数量
        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
     /*  MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
      childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;*/
            childWidth = child.getMeasuredWidth();
            child.layout(childLeft, 0, childLeft + childWidth, height);
            childLayout(child);
            childLeft += childWidth;
        }
    }

    /**
     *让子View的子View和子View大小一样
     */
    private void childLayout(View child){
        if (child==null){
            throw new IllegalStateException("ScrollViewGroup must has one child");
        }
        if (child instanceof ViewGroup){
            if(((ViewGroup)child).getChildCount()>0)
                throw new IllegalStateException("view can host only one direct child");
            ((ViewGroup) child).getChildAt(0).layout(0,0,child.getWidth(),child.getHeight());
        }

    }
//    @Override
//    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
////        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//        View childAt = getChildAt(0);
//        if (childAt instanceof FlagTextView){
//            FlagTextView textView= (FlagTextView) childAt;
//            measureChildcc(textView,widthMeasureSpec,textView.getHHeight());
//        }
//    }
//
//    /**
//     * 测量某一个child的宽高
//     */
//    protected void measureChildcc (FlagTextView child, int parentWidthMeasureSpec, int parentHeightMeasureSpec) {
//        final LayoutParams lp = (LayoutParams) child.getLayoutParams();
//        //获取子控件的宽高约束规则
//        final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
//                getPaddingLeft(), lp. width);
//        final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
//                getPaddingTop() ,child.getHHeight());
//        Log.e("Tag",child.getHHeight()+"-----");
//        child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
//    }
}
